using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using GuavaData;

namespace GuavaLib
{
    public partial class GRadioArray : UserControl,Guava
    {
        string gname;
#if (SPARCE)       
        private ContextMenu popUpMenu;
        private MenuItem markItem;
#endif
        Dictionary<string, RadioButton> m_buttonControlList;
        string[] m_strList;
        int m_HorizontalSpacing = 7;
        int m_VerticalSpacing = 20;
        string m_selectedItem = "";
        int m_ButtonHeight = 20;
        int m_ButtonWidth = 12;

        public GRadioArray()
        {
            InitializeComponent();
            if (null == m_buttonControlList)
            {
                m_buttonControlList = new Dictionary<string,RadioButton>();
                m_strList = new String[2];
                m_strList[0] = "radioButton1";
                m_strList[1] = "radioButton2";
                DrawRadioButtons();
                ResizeRadioControl();
            }
#if (SPARCE)       
            this.markItem = new MenuItem("Mark", new EventHandler(markSelection));
            CreateMenu();
            this.ContextMenu = popUpMenu;
#endif
            }

        private void radioArray1_Load(object sender, EventArgs e)
        {
            ClearPanel();
            DrawRadioButtons();
            ResizeRadioControl();
            SelectRadioItem();
        }

#if (SPARCE)       
        public void CreateMenu()
        {
            popUpMenu = new ContextMenu();
            popUpMenu.MenuItems.Add(markItem);
            // checkQueryMethod = new MenuItem("Query Method", new EventHandler(popup));
            // popUpMenu.MenuItems.Add(checkReturn);
            // popUpMenu.MenuItems.Add(checkQueryMethod);
            // checkQueryMethod.MenuItems.AddRange(regEx);
        }
        private void markSelection(object sender, EventArgs e)
        {
            // Verify that 'mark' was selected
            MenuItem miClicked = sender as MenuItem;
            if (miClicked.Text != "Mark") throw new Exception("Did not understand menu selection " + miClicked.Text);

            // Grab the current GSeed
            Control p = this;
            Control q = p.Parent;
            List<string> ls = new List<string>();
            ls.Add(this.GName);

            // Follow parents up the chain to the root form
            while (!(q is GuavaForm))
            {
                if (!(q is Guava))
                {
                    Guava r = q.Parent as Guava;
                    if (r == null) throw new Exception("Found two non-guava objects nested together in the same tree for marking.");
                    bool bfound = false;
                    foreach (Guava g in r.GComponents)
                    {
                        if (g.GComponents.Contains(p as Guava))
                        {
                            ls.Insert(0, g.GName);
                            bfound = true;
                            break;
                        }
                    }
                    if (!bfound) throw new Exception("Could not match a guava object for creating a seed for marking.");
                }
                else ls.Insert(0, (q as Guava).GName);
                p = p.Parent;
                q = q.Parent;
            }

            // Grab current seed and seed frame
            GSeed gs = (q as GuavaForm).GS;
            GSeedFrame gsf = gs.Peek();
            gsf.controls = ls;

            // Send mark to SPARCE
            SPARCEClipboard.ClipboardClass cc = new SPARCEClipboard.ClipboardClass();
            cc.CopyFodder("GuavaLauncher.GuavaLauncher", "Guava", "GuavaForms", gs.root,
                AppData.assemblyPath, "GuavaSeed", gs.ToString(), gs.formPath + ":" + this.GText,
                this.SelectedItem, Environment.MachineName,
                Environment.UserName, DateTime.Now.ToString(), false);
        }
#endif
        #region Guava Members

        [Browsable(false)]
        public LaunchType LType
        {
            get
            {
                return LaunchType.NA;
            }
        }

        [Browsable(false)]
        public ControlType CType
        {
            get
            {
                return ControlType.Attribute;
            }
        }

        public string GText
        {
            get
            {
                return this.radioGroupBox.Text;
            }

            set
            {
                this.radioGroupBox.Text = value;
            }
        }

        public string GName
        {
            get
            {
                if (gname == null) return this.Name;
                return gname;
            }
            set
            {
                gname = value;
            }
        }

        [Browsable(false)]
        public Point GLocation
        {
            get { return this.Location; }
        }

        [Browsable(false)]
        public Size GSize
        {
            get { return this.Size; }
        }

        [Browsable(false)]
        public List<Guava> GComponents
        {
            get { return new List<Guava>(); }
        }

        public void GBind(string table, int i)
        {
            this.DataBindings.Clear();
            this.DataBindings.Add("SelectedItem", AppData.ds.Tables[table], this.GName);
        }

        [Browsable(false)]
        public string GDefault
        {
            get { return this.SelectedItem; }
        }

        [Browsable(false)]
        public string GToolTip
        {
            get
            {
                Control p = this.ParentForm;
                if (!(p is Guava)) return null;
                ToolTip t = (p as GuavaForm).GToolTipProvider;
                if (t == null) return null;

                // Fetch the tooltip and return it
                return t.GetToolTip(this);
            }
        }

        [Browsable(false)]
        public GuavaData.Domain.Domain GDomain
        {
            get
            {
                return new GuavaData.Domain.Enumerated(m_strList);
            }
        }

        public Guava this[string s]
        {
            get
            {
                foreach (Guava g in this.GComponents) if (g.GName == s) return g;
                return null;
            }
        }
        #endregion

        #region "Radio Array Properties"

        [Category("GRadioArray"),
        Description("Gets/Sets the value to be checked in the radio list"),
        Browsable(true)]
        public string SelectedItem
        {
            get
            {
                return m_selectedItem;
            }
            set
            {
                m_selectedItem = value;
                SelectRadioItem();
            }
        }

        [Category("GRadioArray"),
        Description("Specifies the starting location of the radio control"),
        Browsable(true)]
        public int HorziontalSpacing
        {
            get
            {
                return m_HorizontalSpacing;
            }
            set
            {
                m_HorizontalSpacing = value;
                ClearPanel();
                DrawRadioButtons();
                SelectRadioItem();
            }
        }

        [Category("GRadioArray"),
        Description("Specifies the vertical spacing between the two radio controls."),
        Browsable(true)]
        public int VerticalSpacing
        {
            get
            {
                return m_VerticalSpacing;
            }
            set
            {
                m_VerticalSpacing = value;
                ClearPanel();
                DrawRadioButtons();
                SelectRadioItem();
            }
        }

        [Category("GRadioArray"),
        Description("Specifies string array of the radio button names"),
        Browsable(true)]
        public string[] Buttons
        {
            get
            {
                return m_strList;
            }

            set
            {
                ClearPanel();
                Array.Clear(m_strList, 0, m_strList.Length);
                Array.Resize(ref m_strList, value.Length);
                Array.Copy(value, 0, m_strList, 0, value.Length);
                DrawRadioButtons();
                ResizeRadioControl();

            }
        }
        #endregion

        #region "events"
        private void RadioCheckChanged(object sender, EventArgs e)
        {
            RadioButton radioButton = (RadioButton)sender;
            if (radioButton.Checked)
                m_selectedItem = radioButton.Text;
        }
        #endregion

        #region "Radio Array Private Methods"

        private void DrawRadioButtons()
        {
            Graphics g = this.CreateGraphics();
            int nVerticalSpacing = m_VerticalSpacing;
            for (int i = 0; i < m_strList.Length; i++)
            {
                if (!m_buttonControlList.ContainsKey(m_strList[i]))
                {
                    RadioButton radioButton = new RadioButton();
                    SizeF size = g.MeasureString(m_strList[i], radioButton.Font);
                    radioButton.CheckedChanged += new System.EventHandler(this.RadioCheckChanged);
                    radioButton.Text = m_strList[i];
                    radioButton.AutoSize = false;
                    radioButton.Parent = radioGroupBox;
                    radioButton.Top = nVerticalSpacing;
                    radioButton.Left = m_HorizontalSpacing;
                    radioButton.Width = (Convert.ToInt16((size.Width) + 0.5f)) + 30;
                    nVerticalSpacing += m_VerticalSpacing;
                    m_buttonControlList[m_strList[i]] = radioButton;
                }
            }
        }

        private void ClearPanel()
        {
            this.m_buttonControlList.Clear();
            radioGroupBox.Controls.Clear();
        }

        private void SelectRadioItem()
        {
            if (m_buttonControlList.ContainsKey(m_selectedItem))
            {
                RadioButton rdControl = m_buttonControlList[m_selectedItem];
                if (null != rdControl) rdControl.Checked = true;
            }
        }

        private void ResizeRadioControl()
        {
            int radioTextSize = 0;
            Graphics g = this.CreateGraphics();
            for (int i = 0; i < m_strList.Length; i++)
            {
                SizeF stringSize = g.MeasureString(m_strList[i], this.Font);
                int width = Convert.ToInt16(stringSize.Width);
                if (radioTextSize < width)
                {
                    radioTextSize = width;
                }
            }
            int panelHeight = (m_ButtonHeight * m_strList.Length) + (int)(m_VerticalSpacing * 1.5);
            this.Size = new System.Drawing.Size(radioTextSize + m_ButtonWidth, panelHeight);
        }

        #endregion

        private void radioGroupBox_Enter(object sender, EventArgs e)
        {

        }
    }
}
